package top.oylan.manager.tools;

import org.springframework.stereotype.Component;
import top.oylan.manager.entity.Action;
import top.oylan.manager.service.ActionService;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-02-11
 */
@Component
@WebFilter(urlPatterns = "/admin/*", filterName = "menuFilter")
public class MenuFilter implements Filter {

    @Resource
    ActionService actionService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        List<Action> menus = actionService.selectActionByParentId(0);
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        request.setAttribute("parentMenus",menus);
        filterChain.doFilter(request,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
